package com.example.app.manager

import android.annotation.SuppressLint
import android.graphics.Typeface
import androidx.core.view.isVisible
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.TimeUtils
import com.example.app.R
import com.wheelpicker.DatePicker
import com.wheelpicker.annotation.DateMode
import com.wheelpicker.contract.OnDatePickedListener
import com.wheelpicker.entity.DateEntity


/**
 * CreateBy:Joker
 * CreateTime:2024/8/21/周三 15:08
 * description：
 */
object BirthdayPickerManager {

    @SuppressLint("StaticFieldLeak")
    private var birthdayPicker: DatePicker? = null

    @SuppressLint("SetTextI18n")
    fun showBirthdayPicker(defaultTime: String? = null, onDatePickedListener: OnDatePickedListener) {
        if (birthdayPicker == null) {
            ActivityUtils.getTopActivity()?.let {
                val today = DateEntity.today()
                var dateEntity = DateEntity.target(2000, 1, 1)

                runCatching {
                    if (!defaultTime.isNullOrEmpty()) {
                        val defaultDate = TimeUtils.string2Date(defaultTime, "yyyy-MM-dd")
                        dateEntity = DateEntity.target(defaultDate)
                    }
                }

                birthdayPicker = DatePicker(it).apply {
                    wheelLayout.apply {
                        setAtmosphericEnabled(true) //设置是否启用透明度渐变效果
                        setCurtainEnabled(true) //设置是否启用选中条目的背景
                        setCurtainColor(ColorUtils.getColor(R.color.color_white))//设置选中条目的背景色
                        //setCurvedEnabled() //设置是否启用弯曲效果
                        setCyclicEnabled(false) //设置是否启用无限循环滚动
                        setIndicatorEnabled(false) //设置是否启用条目指示器双横线
                        setSelectedTextBold(true) //设置条目选中项文本加粗
                        setSelectedTextColor(ColorUtils.getColor(R.color.color_black))
                        setSelectedTextSize(ConvertUtils.dp2px(18f).toFloat())
                        setTextColor(ColorUtils.getColor(R.color.color_white)) //设置条目未选中时的文本颜色
                        setTextSize(ConvertUtils.dp2px(17f).toFloat())
                        setDateMode(DateMode.YEAR_MONTH_DAY)
                        setRange(DateEntity.target(1970, 1, 1), DateEntity.target(today.year, today.month, today.day), dateEntity)
                        setResetWhenLinkage(false) //是否联动时复位
                    }

                    titleView.apply {
                        setTextColor(ColorUtils.getColor(R.color.color_black))
                        typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                        textSize = 16f
                        text = "birthday"
                    }

                    cancelView.isVisible = false

                    okView.apply {
                        setTextColor(ColorUtils.getColor(R.color.color_white))
                        textSize = 14f
                        text = "confirm"
                    }
                    contentView.setBackgroundResource(R.color.color_white)

                    setBodyWidth(300)
                    setOnDatePickedListener(onDatePickedListener)
                }
            }
        }

        birthdayPicker?.show()
    }

    fun dismissBirthdayPicker() {
        birthdayPicker?.setOnDatePickedListener(null)
        birthdayPicker?.dismiss()
        birthdayPicker = null
    }
}